﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class AddDuAn : System.Web.UI.UserControl
    {
        public string filePathNewsIamge;
        public int idnextnew;
        DataTable dt;
        ListItem lt;
        protected void Page_Load(object sender, EventArgs e)
        {
            Cate1.Items.Clear();
            dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Select * from tbl_Category WHERE Category_ID2 = 19", true);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                Cate1.Items.Add(lt);
            }
        }

        protected void btnAddNews_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_DuAn", "DuAn_ID"));
            if (fulImageNews.HasFile)
            {
                if (LIB.Validate.CheckFileType(fulImageNews.FileName))
                {
                    if (txtTitle.Text != "")
                    {
                        if (ckeSummary.Text != "")
                        {
                            if (CKEditorNoidung.Text != "")
                            {
                                idnextnew = nextid + 1;
                                int catid = Convert.ToInt32(Cate1.SelectedValue);
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathNewsIamge = "/Resources/Images/Images-Duan/" + fulImageNews.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-Duan/"), fulImageNews.FileName, 200, fulImageNews.PostedFile.InputStream);
                                string EncodeNoidung = Server.HtmlEncode(CKEditorNoidung.Text);
                                string value = "" + idnextnew + "," + catid + ",N'" + txtTitle.Text + "',N'" + txtTitle.Text + "',N'" + filePathNewsIamge + "',N'" + txtAlt.Text + "',N'" + ckeSummary.Text + "',N'" + EncodeNoidung + "','" + DateTime.Now.ToString() + "'";
                                List<string> values = new List<string>();
                                values.Add(value);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_DuAn", "insertonerow", values, false);
                                Response.Redirect("/Admin/Index.aspx?panel=3&sub=30");
                            }
                            else
                            {
                                Response.Write("<script>alert('Bạn vui lòng nhập tiêu đề dự án')</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('Vui lòng nhập tóm tắt nội dung dự án')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Bạn vui lòng nhập nội dung dự án')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('Bạn vui lòng upload ảnh minh họa')</script>");
            }
        }
    }
}